import { typeOf } from '@u/common.js';
import { setTmpCache, getTmpCache } from '@u/cache.js';

function getIsFoldSide() {
  return JSON.parse(getTmpCache('isFoldSide'));
}

const state = {
    isFoldSide: typeOf(getIsFoldSide()) == 'boolean' ? getIsFoldSide() : false,
    isMobile: false,
    withSideAnimation: true,
    isShowLayoutMask: false,
    sideMenuList: [],
    affixTagViews: [],
    tagViews: [],
    aliveRoters: [],
    pages: {},
    scrollCache: {},
    isManuallyRefresh: false
  },
  mutations = {
    foldSide: (state, isFold) => {
      state.isFoldSide = isFold;
      setTmpCache('isFoldSide', isFold);
    },
    resizeToMobile: (state, isMobile) => {
      state.isMobile = isMobile;
    },
    addSideAnimation: (state, isWith) => {
      state.withSideAnimation = isWith;
    },
    showLayoutMask: (state, isShow) => {
      state.isShowLayoutMask = isShow;
    },
    setSideMenuList: (state, list) => {
      state.sideMenuList = list;
    },
    setAffixTagViews: (state, arr) => {
      state.affixTagViews = arr;
      const list = arr.concat(state.tagViews),
        result = [];
      list.map(r => {
        result.push(r.name);
      });
      state.aliveRoters = result;
    },
    editAffixTagViews: (state, tag) => {
      const i = state.affixTagViews.findIndex(t => t.name == tag.name);
      if (i != -1) {
        state.affixTagViews[i] = {
          ...state.affixTagViews[i],
          ...tag,
        };
      }
    },
    setTagViews: (state, arr) => {
      state.tagViews = arr;
      const list = state.affixTagViews.concat(arr),
        result = [];
      list.map(r => {
        result.push(r.name);
      });
      state.aliveRoters = result;
    },
    editTagViews: (state, tag) => {
      const i = state.tagViews.findIndex(t => t.name == tag.name);
      if (i != -1) {
        state.tagViews[i] = {
          ...state.tagViews[i],
          ...tag,
        };
      }
    },
    addTagViews: (state, tag) => {
      const list = state.affixTagViews.concat(state.tagViews);
      if (list.findIndex(t => t.name == tag.name) == -1) {
        state.tagViews.push(tag);
      }
      if (state.aliveRoters.findIndex(a => a == tag.name) == -1) {
        state.aliveRoters.push(tag.name);
      }
    },
    removeTagViews: (state, tag) => {
      const i = state.tagViews.findIndex(t => t.name == tag.name),
        aIdx = state.aliveRoters.findIndex(a => a == tag.name);
      if (i != -1) {
        state.tagViews.splice(i, 1);
      }
      if (aIdx != -1) {
        state.aliveRoters.splice(aIdx, 1);
      }
    },
    addAliveRoters: (state, router) => {
      if (state.aliveRoters.findIndex(a => a == router) == -1) {
        state.aliveRoters.push(router);
      }
    },
    removeAliveRoters: (state, router) => {
      const i = state.aliveRoters.findIndex(r => r == router);
      if (i != -1) {
        state.aliveRoters.splice(i, 1);
      }
    },
    clearAliveRoters: (state) => {
      state.aliveRoters = [];
    },
    setPages: (state, obj) => {
      const result = {
        ...state.pages,
        ...obj
      };
      state.pages = result;
    },
    removePages: (state, name) => {
      delete state.pages[name];
    },
    clearPages: (state) => {
      state.pages = {};
    },
    setScrollCache: (state, cache) => {
      state.scrollCache = {
        ...state.scrollCache,
        ...cache
      };
    },
    clearScrollCache: (state) => {
      state.scrollCache = {};
    },
    setManuallyRefresh: (state, isRefresh) => {
      state.isManuallyRefresh = isRefresh;
    },
    initManuallyRefresh: (state, callback) => {
      if (!state.isManuallyRefresh) {
        callback && callback();
      } else {
        state.isManuallyRefresh = false;
      }
    }
  };

export default {
  namespaced: true,
  state,
  mutations
};
